import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/intro',
    meta: {
      requireAuth: true,
      accessible: [0,1]
    },
    children: [{
      path: 'intro',
      name: 'Intro',
      component: () => import('@/views/intro/index'),
      meta: {
        title: '首页', icon: 'dashboard',
        requireAuth: true
      }
    }]
  },
  {
    path: '/prepare',
    component: Layout,
    redirect: '/prepare/semester',
    name: 'Prepare',
    meta: {
      title: '排课准备工作', icon: 'el-icon-s-help',
      requireAuth: true,
      accessible: [0, 1]
    },
    children: [
      {
        path: 'semester',
        name: 'Semester',
        component: () => import('@/views/prepare/semester/index'),
        meta: { title: '创建学期', icon: 'table' }
      },
      {
        path: 'uploadTask',
        name: 'UploadTask',
        component: () => import('@/views/prepare/uploadTask/index'),
        meta: { title: '上传排课任务表', icon: 'form' }
      },
      {
        path: 'uploadClass',
        name: 'UploadClass',
        component: () => import('@/views/prepare/uploadClass/index'),
        meta: { title: '上传班级表', icon: 'form' }
      },
      {
        path: 'uploadRoom',
        name: 'UploadRoom',
        component: () => import('@/views/prepare/uploadRoom/index'),
        meta: { title: '上传教室表', icon: 'form' }
      }
    ]
  },
  {
    path: '/scheduling',
    component: Layout,
    name: 'Scheduling',
    meta: {
      title: '排课', icon: 'el-icon-s-help'
    },
    children: [
      {
        path: 'auto',
        name: 'Auto',
        component: () => import('@/views/scheduling/auto/index'),
        meta: { title: '排课任务', icon: 'form' }
      },
      {
        path: 'handle',
        name: 'Handle',
        component: () => import('@/views/scheduling/handle/index'),
        meta: { title: '手动解决冲突', icon: 'form' }
      }
    ]
  },
  {
    path: '/table',
    component: Layout,
    name: 'Table',
    meta: {
      title: '课表', icon: 'el-icon-s-help'
    },
    children: [
      {
        path: 'classTable',
        name: 'ClassTable',
        component: () => import('@/views/table/classTable/index'),
        meta: { title: '班级课表', icon: 'form' }
      },
      {
        path: 'teacherTable',
        name: 'TeacherTable',
        component: () => import('@/views/table/teacherTable/index'),
        meta: { title: '教师课表', icon: 'form' }
      },
      {
        path: 'roomTable',
        name: 'RoomTable',
        component: () => import('@/views/table/roomTable/index'),
        meta: { title: '教室课表', icon: 'form' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
